package com.lzl.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzl.eduservice.entity.EduSubject;
import com.lzl.eduservice.entity.excel.SubjectData;
import com.lzl.eduservice.service.EduSubjectService;
import com.lzl.servicebase.exceptionHandler.LZLException;

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    /**
     * 因为SubjectExcelListener不能交给spring进行管理(不是很理解)，需要自己new（在serviceImpl方法中new了）
     * 不能注入其他对象
     * 不能实现数据库操作
     * 现在通过有参构造来传参
     * 在你newSubjectExcelListener对象时，把eduSubjectService参数传过去
     */
    public EduSubjectService eduSubjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData == null) {
            throw new LZLException(20001, "文件数据为空");
        }
        //读取excel一行一行读取，每次读取有两个值，第一个是一级分类，第二个是二级分类
        //判断一级分类是否重复添加
        EduSubject existOneSubject = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
        if (existOneSubject == null) {
            //没有相同一级的分类，进行添加
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getOneSubjectName());
            eduSubjectService.save(existOneSubject);
        }
        //这个id值是一定会有的，existOneSubject不为空即有值，existOneSubject为空时，进行上面的判断，也会有值了
        String pid = existOneSubject.getId();
        //添加二级分类
        //判断二级分类是否重复添加
        EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), pid);
        if (existTwoSubject == null) {
            //没有相同二级的分类，进行添加
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(pid);
            //二级分类名称
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());
            eduSubjectService.save(existTwoSubject);
        }


    }

    //判断一级分类不能重复添加
    public EduSubject existOneSubject(EduSubjectService eduSubjectService, String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name);
        wrapper.eq("parent_id", "0");
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);
        return oneSubject;
    }

    //判断二级分类不能重复添加
    public EduSubject existTwoSubject(EduSubjectService eduSubjectService, String name, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name);
        wrapper.eq("parent_id", pid);
        EduSubject twoSubject = eduSubjectService.getOne(wrapper);
        return twoSubject;
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
